//
//  LMLocalAccountManager.m
//  LM
//
//  Created by Kien Tran on 2/4/13.
//  Copyright (c) 2013 Kien Tran. All rights reserved.
//

#import "LMLocalAccountManager.h"
#import "LMLocalUser.h"
#import "LMLocalPartner.h"
#import "LMConstant.h"

@implementation LMLocalAccountManager

static __weak LMLocalPartner *_selectedPartner;
static NSMutableArray *_partnerArray;

static NSInteger limitPartner = 2;

+ (LMLocalPartner *) sharedSelectedPartner {
    if (!_partnerArray) {
        [LMLocalAccountManager loadPartnerFromDisk];
    }
    return _selectedPartner;
}

+ (NSMutableArray *) sharedPartnerArray {
    if (!_partnerArray) {
        [LMLocalAccountManager loadPartnerFromDisk];
    }
    
    return _partnerArray;
}

+ (BOOL) canCreatePartner {
    BOOL canCreate = NO;
    
    if (_partnerArray && [_partnerArray count] < limitPartner) {
        canCreate = YES;
    }
    
    return canCreate;
}

+ (BOOL) createPartnerWithName:(NSString *)name sex:(BOOL)isMale  birthday:(NSDate *)birthday phone:(NSString *) phone facebook:(NSString *) facebook datingFirstDate:(NSDate *) datingFirstDate {
    
    BOOL isCreated = NO;
    if (!_partnerArray || [_partnerArray count] == 0) {
        LMLocalPartner *fristPartner = [[LMLocalPartner alloc]initWithName:name sex:isMale birthday:birthday phone:phone facebook:facebook datingFirstDate:datingFirstDate];
        fristPartner.ID = 1;
        _selectedPartner = fristPartner;
        
        _partnerArray = [NSMutableArray arrayWithObject:fristPartner];
        
        isCreated = YES;
    } else {
        if ([_partnerArray count] >= limitPartner) {
            isCreated = NO;
        } else {
            LMLocalPartner *partner = [[LMLocalPartner alloc]initWithName:name sex:isMale birthday:birthday phone:phone facebook:facebook datingFirstDate:datingFirstDate];
            partner.ID = [[_partnerArray lastObject] ID] + 1;
            
            
            [_partnerArray addObject:partner];
            
            isCreated = YES;
        }
    }
    
    [LMLocalAccountManager savePartnerToDisk];
    return isCreated;
}

+ (LMLocalPartner *)partnerAtIndex:(NSInteger)index {
    if (!_partnerArray || [_partnerArray count] <= index) {
        return nil;
    }
    
    return [_partnerArray objectAtIndex:index];
}

+ (LMLocalPartner *) partnerWithID:(NSInteger) ID{
    if (!_partnerArray) {
        return nil;
    }
    
    for (LMLocalPartner *partner in _partnerArray) {
        if (partner.ID == ID) {
            return  partner;
        }
    }
    
    return nil;
}

+ (LMLocalPartner *) setSelectedPartner:(LMLocalPartner *) partner {
    if (!partner || !_partnerArray) {
        return nil;
    }
    
    LMLocalPartner *selectedPartner = [LMLocalAccountManager partnerWithID:partner.ID];
    
    if (selectedPartner) {
        _selectedPartner = selectedPartner;
        
        //Save selected ID to disk
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];        
        [userDefault setInteger:_selectedPartner.ID forKey:kKEY_LOCAL_SELETECTED_PARTNER];
        return [LMLocalAccountManager sharedSelectedPartner];
    } else {
        return nil;
    }
    
}


+ (BOOL) editPartnerWithNewPartner:(LMLocalPartner *)partner {
    if (!partner || !_partnerArray) {
        return NO;
    }
    
    for (int i = 0; i < [_partnerArray count]; i++) {
        LMLocalPartner *p = [_partnerArray objectAtIndex:i];
        if (p.ID == partner.ID) {
            [p copy:partner];
            
            [LMLocalAccountManager savePartnerToDisk];
            return YES;
        }
    }
    
    return NO;
}

+ (void) savePartnerToDisk {
    if (_partnerArray) {
        NSMutableArray *dicArray = [NSMutableArray array];
        
        for (LMLocalPartner *partner in _partnerArray) {
            [dicArray addObject:[partner dictionary]];
        }
        
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        [userDefault setObject:dicArray forKey:kKEY_LOCAL_PARTNERS];
        
        [userDefault setInteger:_selectedPartner.ID forKey:kKEY_LOCAL_SELETECTED_PARTNER];
    }
}

+ (void)            loadPartnerFromDisk {
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];

    NSMutableArray *dicArray = [userDefault objectForKey:kKEY_LOCAL_PARTNERS];
    _partnerArray = [NSMutableArray array];
    if (dicArray) {
        for (NSDictionary *dic in dicArray) {
            [_partnerArray addObject:[[LMLocalPartner alloc] initWithDictionary:dic]];
        }
        
        NSInteger selectedID = [userDefault integerForKey:kKEY_LOCAL_SELETECTED_PARTNER];
        _selectedPartner = [LMLocalAccountManager partnerWithID:selectedID];
    }

}

+ (void)            removePartnerWithIndex:(NSInteger) index {
    id removedObject = [_partnerArray objectAtIndex:index];
    
    [_partnerArray removeObjectAtIndex:index];
    
    if (_selectedPartner == removedObject) {
        if ([_partnerArray count] == 0) {
            _selectedPartner = nil;
        } else {
            _selectedPartner = [_partnerArray objectAtIndex:0];
        }
    }
   
    
    
    [LMLocalAccountManager savePartnerToDisk];
}
@end
